package cn.edu.wfit.modules.learn.service.impl.competitionmanagement;

import cn.edu.wfit.common.core.utils.DateUtils;
import cn.edu.wfit.modules.learn.domain.WfitCompetition;
import cn.edu.wfit.modules.learn.domain.WfitCompetitionIndividualrecord;
import cn.edu.wfit.modules.learn.domain.WfitCompetitionIndividualregistration;
import cn.edu.wfit.modules.learn.domain.dto.IndividualDetailDto;
import cn.edu.wfit.modules.learn.mapper.competitionmanagement.WfitCompetitionIndividualrecordMapper;
import cn.edu.wfit.modules.learn.mapper.competitionmanagement.WfitCompetitionIndividualregistrationMapper;
import cn.edu.wfit.modules.learn.mapper.competitionmanagement.WfitCompetitionMapper;
import cn.edu.wfit.modules.learn.service.competitionmanagement.IWfitCompetitionIndividualregistrationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 个人赛报名Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-28
 */
@Service
public class WfitCompetitionIndividualregistrationServiceImpl implements IWfitCompetitionIndividualregistrationService {
    @Autowired
    private WfitCompetitionIndividualregistrationMapper wfitCompetitionIndividualregistrationMapper;
    @Autowired
    private WfitCompetitionMapper wfitCompetitionMapper;
    @Autowired
    private WfitCompetitionIndividualrecordMapper wfitCompetitionIndividualrecordMapper;

    /**
     * 查询个人赛报名
     *
     * @param id 个人赛报名主键
     * @return 个人赛报名
     */
    @Override
    public WfitCompetitionIndividualregistration selectWfitCompetitionIndividualregistrationById(Long id) {
        return wfitCompetitionIndividualregistrationMapper.selectWfitCompetitionIndividualregistrationById(id);
    }

    /**
     * 查询个人赛报名列表
     *
     * @param wfitCompetitionIndividualregistration 个人赛报名
     * @return 个人赛报名
     */
    @Override
    public List<WfitCompetitionIndividualregistration> selectWfitCompetitionIndividualregistrationList(WfitCompetitionIndividualregistration wfitCompetitionIndividualregistration) {
        return wfitCompetitionIndividualregistrationMapper.selectWfitCompetitionIndividualregistrationList(wfitCompetitionIndividualregistration);
    }

    /**
     * 新增个人赛报名
     *
     * @param wfitCompetitionIndividualregistration 个人赛报名
     * @return 结果
     */
    @Override
    public int insertWfitCompetitionIndividualregistration(WfitCompetitionIndividualregistration wfitCompetitionIndividualregistration) {

        List<WfitCompetitionIndividualregistration>  individualRecordExist = wfitCompetitionIndividualregistrationMapper.selectWfitCompetitionIndividualregistrationList(wfitCompetitionIndividualregistration);
        if(individualRecordExist.isEmpty()){
            wfitCompetitionIndividualregistration.setCreateTime(DateUtils.getNowDate());
            return wfitCompetitionIndividualregistrationMapper.insertWfitCompetitionIndividualregistration(wfitCompetitionIndividualregistration);
        }else{
            throw new RuntimeException("您已报名此竞赛，无须再次报名");
        }


    }

    /**
     * 修改个人赛报名
     *
     * @param wfitCompetitionIndividualregistration 个人赛报名
     * @return 结果
     */
    @Override
    public int updateWfitCompetitionIndividualregistration(WfitCompetitionIndividualregistration wfitCompetitionIndividualregistration) {
        wfitCompetitionIndividualregistration.setUpdateTime(DateUtils.getNowDate());
        return wfitCompetitionIndividualregistrationMapper.updateWfitCompetitionIndividualregistration(wfitCompetitionIndividualregistration);
    }

    /**
     * 批量删除个人赛报名
     *
     * @param ids 需要删除的个人赛报名主键
     * @return 结果
     */
    @Override
    public int deleteWfitCompetitionIndividualregistrationByIds(Long[] ids) {
        return wfitCompetitionIndividualregistrationMapper.deleteWfitCompetitionIndividualregistrationByIds(ids);
    }

    /**
     * 删除个人赛报名信息
     *
     * @param id 个人赛报名主键
     * @return 结果
     */
    @Override
    public int deleteWfitCompetitionIndividualregistrationById(Long id) {
        return wfitCompetitionIndividualregistrationMapper.deleteWfitCompetitionIndividualregistrationById(id);
    }

    /**
     * 根据学生id查询个人赛报名信息
     *
     * @param studentId
     * @return
     */
    @Override
    public List<IndividualDetailDto> IndividualListByStudentId(String studentId) {
        //创建一个List用于存储封装好的CompetitionDetailDto
        List<IndividualDetailDto> competitionDetailDtoList = new ArrayList<>();

        WfitCompetitionIndividualregistration wfitCompetitionIndividualregistration = new WfitCompetitionIndividualregistration();
        //根据学号查询报名表中的个人赛记录id
        wfitCompetitionIndividualregistration.setStudentId(Long.valueOf(studentId));
        List<WfitCompetitionIndividualregistration> wfitCompetitionIndividualregistrations = wfitCompetitionIndividualregistrationMapper.selectWfitCompetitionIndividualregistrationList(wfitCompetitionIndividualregistration);
        //通过个人赛的记录id查询个人赛记录的信息,并封装到CompetitionDetailDto中
        for (WfitCompetitionIndividualregistration individualregistration : wfitCompetitionIndividualregistrations) {
            IndividualDetailDto competitionDetailDto = new IndividualDetailDto();
            //活动记录表信息
            WfitCompetitionIndividualrecord Individualrecord = wfitCompetitionIndividualrecordMapper.selectWfitCompetitionIndividualrecordById(individualregistration.getIndividualContestId());
            //将记录表信息封装到CompetitionDetailDto中
            BeanUtils.copyProperties(Individualrecord, competitionDetailDto);
            //根据记录id查询竞赛表的信息
            WfitCompetition competition = wfitCompetitionMapper.selectWfitCompetitionById(Individualrecord.getCompetitionId());
            //将竞赛表信息封装到CompetitionDetailDto中
            competitionDetailDto.setCompetition(competition);
            //将封装好的CompetitionDetailDto添加到List中
            competitionDetailDtoList.add(competitionDetailDto);
        }
        return competitionDetailDtoList;
    }

}
